home *** CD-ROM | disk | FTP | other *** search
/ Corel Draw 9 / CorelDraw9 Disco 1.iso / Scripts / Animals.csc next >
Text File  |  1998-07-30  |  6KB  |  237 lines

  1. REM El ordenador adivina los animales y los aprende.
  2. REM Animals.csc 10 de agosto de 1996 qa JB AN
  3. REM Copyright 1996 Corel Corporation. Reservados todos los derechos.
  4.  
  5. ' FUNCTION declarations
  6. DECLARE SUB Intro()
  7. DECLARE SUB EndMessage()
  8. DECLARE SUB LoadDatabase( filename$ )
  9. DECLARE SUB SaveDatabase( filename$ )
  10. DECLARE FUNCTION QuestionUser() AS BOOLEAN
  11. DECLARE SUB ShowSuggestion()
  12. DECLARE FUNCTION Guess( a% ) AS INTEGER
  13. DECLARE SUB FindAnimal()
  14.  
  15. ' GLOBAL variables and constants
  16. GLOBAL CONST MAXDATA%=999       ' Size of database
  17. GLOBAL CONST YES%=6             ' Messagebox yes value
  18. GLOBAL CONST YESNOBUTTONS%=4    ' messagebox YES/NO buttons
  19. GLOBAL CONST TITLE$="Animal Game" ' Game title
  20. GLOBAL anim(MAXDATA) AS STRING  ' holds animal database
  21. GLOBAL animsize AS INTEGER      ' Size of animal database                
  22. GLOBAL CONST FILENAME$ = "animals.gme" ' name of the file
  23.  
  24. '==========================================================
  25. ' Display intro
  26. Intro
  27. ' Fill up databASe
  28. LoadDatabase(FILENAME)
  29. ' question loop
  30. DO 
  31. LOOP WHILE QuestionUser()
  32.  
  33. ' end message
  34. EndMessage
  35.  
  36. '==========================================================
  37. ' Display introduction message
  38. SUB Intro
  39.     DIM temp AS STRING    ' holds temporary string
  40.     temp = "Play GUESS THE ANIMAL with Corel SCRIPT" + CHR(13)
  41.     temp = temp + "Think of an animal and the computer will try to guess it..."
  42.     MESSAGE temp
  43. END SUB
  44.  
  45. '==========================================================
  46. ' Display end message
  47. SUB EndMessage
  48.     DIM temp AS STRING    ' holds temporary string
  49.         IF MESSAGEBOX("Do you want to save this game?",TITLE,YESNOBUTTONS)=YES THEN
  50.         ' Save database
  51.         SaveDatabase(FILENAME)
  52.     END IF
  53.  
  54.     temp = "Thank you for playing."
  55.     MESSAGE  temp 
  56.     STOP
  57. END SUB
  58.  
  59. '==========================================================
  60. ' Try to load database from file.
  61. ' if file is currupted or not present, assign default values
  62. SUB LoadDatabase( filename$ )
  63.     ON ERROR GOTO errorhandler
  64.     DIM a AS INTEGER        ' loop var
  65.  
  66.     OPEN filename FOR INPUT AS 1
  67.     INPUT #1, animsize
  68.     FOR a=1 TO animsize
  69.         INPUT #1, anim(a)
  70.     NEXT a
  71.     CLOSE #1
  72.     
  73.     subend:
  74.     EXIT SUB
  75.     errorhandler:
  76.         ' We could not load database
  77.         ' Assign default values
  78.         CLOSE #1
  79.         animsize=3
  80.         anim(1)="?002003Does it swim?"
  81.         anim(2)="!Gold fish"
  82.         anim(3)="!Hawk"
  83.         resume at subend
  84. END SUB
  85.  
  86. '==========================================================
  87. ' Try to save database to file.
  88. SUB SaveDatabase( filename$ )
  89.     ON ERROR GOTO errorhandler
  90.     DIM a AS INTEGER        ' loop var
  91.  
  92.     OPEN filename FOR OUTPUT AS 1
  93.     WRITE #1, animsize
  94.     FOR a = 1 TO animsize
  95.         WRITE #1, anim(a)
  96.     NEXT a
  97.     CLOSE #1
  98.     
  99.     subend:
  100.     EXIT SUB
  101.     errorhandler:
  102.         ' We could not save database
  103.         CLOSE #1
  104.         MESSAGE "Error saving game!"
  105.         RESUME AT subend
  106. END SUB
  107.  
  108. '==========================================================
  109. ' Question user on a animal
  110. FUNCTION QuestionUser AS BOOLEAN
  111.     DIM ans AS INTEGER            ' holds the answer value
  112.  
  113.     ans = MESSAGEBOX("Are you thinking of an animal?",TITLE,YESNOBUTTONS)
  114.     IF ans=YES THEN
  115.         ' answer is YES
  116.         FindAnimal
  117.     ELSE
  118.         ' answer is NO
  119.         ans = MESSAGEBOX("Can I give you some suggestions?",TITLE,YESNOBUTTONS)
  120.         IF ans=YES THEN ShowSuggestion
  121.     END IF    
  122.     
  123.     ' Decide IF we want to continue
  124.     IF ans<>YES THEN 
  125.         QuestionUser=FALSE
  126.     ELSE
  127.         QuestionUser = TRUE
  128.     END IF
  129. END FUNCTION
  130.  
  131. '==========================================================
  132. ' Prints suggestion list
  133. SUB ShowSuggestion
  134.     DIM mess AS STRING    ' Message buffer
  135.     DIM a AS INTEGER        ' loop counter
  136.     DIM num AS INTEGER  ' number of elements in array
  137.     DIM ani AS INTEGER  ' number of animals found
  138.     
  139.     ani=0
  140.     mess = "Here are some suggestions:" + CHR(13) + CHR(13) + CHR(10)
  141.     
  142.     ' Prints all animals
  143.     FOR a = 1 TO animsize
  144.         IF LEFT( anim(a),1)="!" THEN 
  145.             mess = mess + MID( anim(a),2)
  146.             ani=ani+1
  147.     
  148.             ' Format string
  149.             IF ani MOD 5 = 0 THEN
  150.                 mess = mess + CHR(13)
  151.             ELSE
  152.                 mess = mess + CHR(9)
  153.             END IF
  154.         END IF
  155.     NEXT a
  156.     MESSAGE mess
  157. END SUB
  158.  
  159. '==========================================================
  160. ' Try to guess animal
  161. FUNCTION Guess( question% ) AS INTEGER
  162.     IF MESSAGEBOX(MID(anim(question),8),TITLE,YESNOBUTTONS)=YES THEN
  163.         Guess = CINT(MID(anim(question),2,3))
  164.     ELSE
  165.         Guess = CINT(MID(anim(question),5,3))
  166.     END IF
  167. END FUNCTION
  168.  
  169. '==========================================================
  170. ' Find next question and guess
  171. SUB FindAnimal
  172.     DIM mess AS STRING      ' Message buffer
  173.     DIM g AS INTEGER        ' guess
  174.     DIM answer AS INTEGER   ' tempo answer
  175.     DIM newval AS INTEGER   ' next empty space
  176.     DIM animal AS STRING    ' player animal
  177.     DIM question AS STRING  ' player question
  178.     DIM ret AS INTEGER        ' Cancel or OK
  179.  
  180.     ' Start guessing with first question
  181.     g = 1
  182.     ' find animal
  183.     WHILE LEFT(anim(g),1) = "?"
  184.         g = Guess(g)            
  185.     WEND
  186.     ' Verify guess
  187.     answer=MESSAGEBOX("The animal you were thinking of was a " + MID(anim(g),2) + "?", TITLE,YESNOBUTTONS)
  188.     IF answer=YES OR animsize>MAXDATA-2 THEN
  189.         ' We have it
  190.         MESSAGE "Why not think of another animal?"
  191.     ELSE
  192.       ' Enter in database
  193.       DO
  194.         BEGIN DIALOG textdlg 285, 65, Title$            
  195.             TEXTBOX 10, 25, 200, 30, animal
  196.             TEXT  10, 5, 250, 16, "This animal you were thinking of was a"
  197.             OKBUTTON 225, 25, 50, 14
  198.             CANCELBUTTON 225, 41, 50, 14
  199.         END DIALOG
  200.  
  201.         ret = DIALOG(textdlg)
  202.         ' If Cancel is selected, exit game
  203.         IF ret = 2 THEN EndMessage
  204.         LOOP WHILE animal = ""
  205.  
  206.         
  207.         mess = "Please enter a question that would distinguish a " 
  208.         mess = mess + animal + " from a " + MID(anim(g),2)
  209.         DO
  210.             BEGIN DIALOG textdlg2 285, 65, Title$                
  211.                 TEXTBOX 10, 25, 200, 30, question
  212.                 TEXT  10, 5, 250, 16, mess
  213.                 OKBUTTON 225, 25, 50, 14
  214.                 CANCELBUTTON 225, 41, 50, 14
  215.             END DIALOG
  216.         
  217.         ret = DIALOG(textdlg2)
  218.         ' If Cancel is selected, exit game
  219.         IF ret = 2 THEN EndMessage
  220.         LOOP WHILE question=""
  221.         
  222.         newval = animsize + 1
  223.         answer = MESSAGEBOX("For a " + animal + " the answer would be?",TITLE,YESNOBUTTONS)
  224.         IF answer=YES THEN
  225.             anim(newval) = "!" + animal
  226.             anim(newval+1) = anim(g)
  227.         ELSE
  228.             anim(newval+1) = "!" + animal
  229.             anim(newval) = anim(g)
  230.         END IF
  231.         '  Make sure we have leading 0
  232.         anim(g) = "?" + RIGHT(newval+1000,3) + RIGHT(newval+1001,3) + question
  233.         animsize = newval+1
  234.     END IF
  235. END SUB
  236.  
  237.